package com.zz.websocketclient;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private MyWebSocketClient myWebSocketClient;
    private Handler handler = new Handler(Looper.getMainLooper());
    private TextView info;
    private EditText edit;
    private EditText edit_msg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        info = findViewById(R.id.info);
        edit = findViewById(R.id.edit);
        edit_msg = findViewById(R.id.edit_msg);
    }

    public void start(String uri) {
        try {
            URI url = new URI(uri);
            myWebSocketClient = new MyWebSocketClient(url);
            myWebSocketClient.setReuseAddr(true);
            myWebSocketClient.setConnectionLostTimeout(10);
            myWebSocketClient.connect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sendMessage(View view) {
        String s = edit_msg.getText().toString();
        myWebSocketClient.send(s);
    }

    public void connect(View view) {
        String s = edit.getText().toString();
        start(s);
    }

    private class MyWebSocketClient extends WebSocketClient {

        public MyWebSocketClient(URI serverURI) {
            super(serverURI);
        }

        @Override
        public void onOpen(ServerHandshake serverHandshake) {
            handler.post(() -> {
                info.append(getLocalTime() + "onOpen getHttpStatus = " + serverHandshake.getHttpStatus());
                Toast.makeText(MainActivity.this, "onOpen", Toast.LENGTH_LONG).show();
                Log.i("eeeeee", "onOpen");
            });
        }

        @Override
        public void onMessage(String s) {
            handler.post(() -> {
                info.append(getLocalTime() + "onMessage = " + s);
                Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
                Log.i("eeeeee", s);
            });
        }

        @Override
        public void onMessage(ByteBuffer bytes) {

        }

        @Override
        public void onClose(int code, String reason, boolean remote) {
            handler.post(() -> {
                String s = "onClose: code = " + code + " reason = " + reason + " remote = " + remote;
                info.append(getLocalTime() + s);
                Toast.makeText(MainActivity.this, "socket code = " + code + " reason = " + reason + " remote = " + remote, Toast.LENGTH_LONG).show();
                Log.i("eeeeee", "socket code = " + code + " reason = " + reason + " remote = " + remote);
            });

        }

        @Override
        public void onError(Exception e) {
            handler.post(() -> {
                String s = "onError: e = " + e.getMessage();
                info.append(getLocalTime() + s);
                Toast.makeText(MainActivity.this, "e = " + e.getMessage(), Toast.LENGTH_LONG).show();
                Log.i("eeeeee", "e = " + e.getMessage());
            });
        }
    }


    private String getLocalTime() {
        URL url = null;//取得资源对象
        try {
            DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            String format = formatter.format(calendar.getTime());
            String s = "\n" + format.split(" ")[1] + "   ";
            return s;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}